package seo.dale.util.calculator;

import seo.dale.util.calculator.operation.Operation;
import seo.dale.util.calculator.operation.factory.*;

public class Calculator {
	
	private double curr;
	private double temp;
	
	private Operation operation;
	private OperationFactory factory;

	public void setOpeartionFactory(OperationFactory factory) {
		this.factory = factory;
	}
	
	public void clear() {
		curr = 0.0;		
	}
	
	public void input(double i) {
		curr = i;		
	}
	
	public double output() {
		return curr;
	}

	public void equal() {
		if (operation == null)
			operation = factory.createOperationFor("");
		curr = operation.operate(temp, curr);
	}
	
	public void add() {
		operate("+");
	}

	public void subtract() {
		operate("-");
	}

	public void multiply() {
		operate("*");
	}
	
	public void devide() {
		operate("/");
	}
	
	private void operate(String operator) {
		save();
		operation = factory.createOperationFor(operator);
	}
	
	private void save() {
		temp = curr;
	}

}